package com.edu.service.impl;

import com.edu.beans.Book;
import com.edu.beans.Vip;
import com.edu.dao.BookDao;
import com.edu.dao.VipsDao;
import com.edu.dao.impl.BookDaoImpl;
import com.edu.dao.impl.VipsDaoImpl;
import com.edu.exception.UsedException;
import com.edu.service.VipsService;

import java.util.Collections;
import java.util.List;

public class VipsServiceImpl implements VipsService {

    VipsDao vipsDao = new VipsDaoImpl();
    BookDao bookDao = new BookDaoImpl();

    @Override
    public int addVip(Vip vip) throws UsedException {
        int countCard = vipsDao.getVipsByCard(vip.getCard()).size();
        if (countCard > 0) {
            throw new UsedException("该会员号（身份证号）已经被注册！");
        }
        int countPhone = vipsDao.getVipsByPhone(vip.getPhone()).size();
        if (countPhone > 0) {
            throw new UsedException("该手机号已经被注册！");
        }
        return vipsDao.addVip(vip);
    }

    @Override
    public List<Vip> findVipsByPhone(String phone) {
        return vipsDao.findVipsByPhone(phone);
    }

    @Override
    public int deleteVipById(int id) throws UsedException{
        List<Book> booksByVipId = bookDao.getBooksByVipId(id);
        if (booksByVipId.size() == 0) {
            return vipsDao.deleteVipById(id);
        } else {
            throw new UsedException("该会员有预定记录，不能删除！");
        }
    }

    @Override
    public Vip findVipByid(int id) {
        return vipsDao.findVipByid(id);
    }

    @Override
    public int updateVip(Vip vip) {
        return vipsDao.updateVip(vip);
    }

    @Override
    public List<Vip> getAllVips() {
        return vipsDao.getAllVips();
    }
}
